package application

import (
	"errors"
	"ruoyi-go-echo-ddd/internal/domain/model"
	"ruoyi-go-echo-ddd/internal/domain/service"
	"ruoyi-go-echo-ddd/internal/infrastructure/http/request"

	"github.com/labstack/echo/v4"
)

type SysRoleApplication struct {
	service *service.SysRoleService
}

func NewSysRoleApplication(s *service.SysRoleService) *SysRoleApplication {
	return &SysRoleApplication{
		service: s,
	}
}

func (s *SysRoleApplication) Insert(e echo.Context, dept *model.SysRole) (*model.SysRole, error) {
	return s.service.Insert(e.Request().Context(), dept)
}

func (s *SysRoleApplication) Update(e echo.Context, dept *model.SysRole) (int64, error) {
	return s.service.Update(e.Request().Context(), dept)
}

func (s *SysRoleApplication) UpdateWithOutMenuAndDataScope(e echo.Context, sysRole *model.SysRole) (int64, error) {
	return s.service.UpdateWithOutMenuAndDataScope(e.Request().Context(), sysRole)
}

func (s *SysRoleApplication) BatchInsert(e echo.Context, list []*model.SysRole) ([]int64, error) {
	if len(list) <= 0 {
		return nil, errors.New("系统角色批量新增参数验证失败")
	}
	return s.service.BatchInsert(e.Request().Context(), list)
}

func (s *SysRoleApplication) SelectById(e echo.Context, id int64) (*model.SysRole, error) {
	if id == 0 {
		return nil, errors.New("查询参数验证失败")
	}
	return s.service.SelectById(e.Request().Context(), id)
}

func (s *SysRoleApplication) SelectPage(e echo.Context, param *request.RolePageParam) ([]*model.SysRole, int64, int64, error) {
	return s.service.SelectPage(e.Request().Context(), param)
}

func (s *SysRoleApplication) BatchDelete(e echo.Context, ids []any) (int64, error) {
	if len(ids) <= 0 {
		return 0, errors.New("系统角色批量删除参数验证失败")
	}
	return s.service.BatchDelete(e.Request().Context(), ids)
}

func (s *SysRoleApplication) SelectList(e echo.Context, ids []int64) ([]*model.SysRole, error) {
	if len(ids) <= 0 {
		return nil, errors.New("系统角色查询数验证失败")
	}
	return s.service.SelectList(e.Request().Context(), ids)
}

func (s *SysRoleApplication) SelectIdsByUserId(e echo.Context, userId any) ([]int64, error) {
	if userId == nil {
		return nil, errors.New("系统角色查询角色ID不能为空")
	}
	return s.service.SelectIdsByUserId(e.Request().Context(), userId)
}

func (s *SysRoleApplication) SelectRoleKeyListByUserId(e echo.Context, userId any) ([]string, error) {
	if userId == nil {
		return nil, errors.New("系统角色查询角色ID不能为空")
	}
	return s.service.SelectRoleKeyListByUserId(e.Request().Context(), userId)
}

func (s *SysRoleApplication) SelectRoleNameListByUserId(e echo.Context, userId any) ([]string, error) {
	if userId == nil {
		return nil, errors.New("系统角色查询角色ID不能为空")
	}
	return s.service.SelectRoleNameListByUserId(e.Request().Context(), userId)
}

func (s *SysRoleApplication) SelectAll(e echo.Context) ([]*model.SysRole, error) {
	return s.service.SelectAll(e.Request().Context())
}

func (s *SysRoleApplication) VerifyRoleNameExist(e echo.Context, role *model.SysRole) (bool, error) {
	return s.service.VerifyRoleNameExist(e.Request().Context(), role)
}

func (s *SysRoleApplication) VerifyRoleKeyExist(e echo.Context, role *model.SysRole) (bool, error) {
	return s.service.VerifyRoleKeyExist(e.Request().Context(), role)

}

func (s *SysRoleApplication) VerifyRoleIsAllowed(role *model.SysRole) bool {
	if role == nil {
		return false
	}
	return model.IsAdminRole(role.Id)
}


func (s *SysRoleApplication) SelectAllForExcel(e echo.Context) ([]*model.SysRole, error) {
	return s.service.SelectAllForExcel(e.Request().Context())
}
